package cn.xkai.exercise.c;

import java.util.HashSet;
import java.util.Set;

/**
 * @description: 宝石与石头
 * 自己的思路：数组、hash
 * @author: kaixiang
 * @date: 2022-07-28
 **/
public class Solution104 {

    public int numJewelsInStones(String jewels, String stones) {
        Set<Character> set = new HashSet<>();
        int count = 0;
        for (char jewel : jewels.toCharArray()) {
            set.add(jewel);
        }
        for (char stone : stones.toCharArray()) {
            if (set.contains(stone)) {
                count++;
            }
        }
        return count;
    }

    public int numJewelsInStonesArray(String jewels, String stones) {
        int count = 0;
        boolean[] visited = new boolean[stones.length()];
        for (int i = 0; i < jewels.length(); i++) {
            for (int j = 0; j < stones.length(); j++) {
                if (visited[j]) {
                    continue;
                }
                if (jewels.indexOf(stones.charAt(j)) > -1) {
                    visited[j] = true;
                    count++;
                }
            }
        }
        return count;
    }

}
